/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       creature_select_window.cpp

	$Header: /game/creature_select_window.cpp $

	$NoKeywords: $

 ************************************************************************/
// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "creature_select_window.h"

#include "adaptor_handler.h"
#include "bitmap_group.h"
#include "bitmap_group_cache.h"
#include "bitmap_layer_cache_window.h"
#include "bound_handler.h"
#include "create_creature_button.h"
#include "creature_stack.h"
#include "format_string.h"
#include "text_window.h"
#include "toggle_button.h"

extern t_bitmap_group_cache const k_creature_select_bitmaps( "control.creature_select" );
static const int k_rect_offset = 10;

// ------------------------------------------------------------
// control which allows you to select one or more creatures
// ------------------------------------------------------------
t_creature_select_window::t_creature_select_window( t_screen_point point, bool single_select, 
												    t_window* parent )
						: t_window( k_completely_transparent, parent )
{
	t_screen_rect rect;

	m_bitmaps = k_creature_select_bitmaps.get();
	m_single_select = single_select;
	rect = m_bitmaps->get_rect() + point;
    rect.right += k_rect_offset;
    rect.bottom += k_rect_offset;
	init( rect );
}

// ------------------------------------------------------------
// control which allows you to select one or more creatures
// ------------------------------------------------------------
void t_creature_select_window::add( t_creature_stack const* arg, bool display_only, bool force_awake_portrait )
{
	t_button_handler		handler;
	t_screen_point			point;
	t_toggle_button*		button;
	t_creature_stack*       stack = const_cast<t_creature_stack*>( arg );
	t_bitmap_layer_ptr		icon = stack->get_portrait( 52, force_awake_portrait );
	std::string             text;

	button = new t_toggle_button( t_screen_point(0,0), this, m_single_select );

	if (stack->get_creature() != 0)
		text = format_string( "%i", stack->get_number() );

	create_creature_button( button, m_bitmaps, icon, text, display_only );

	handler = add_2nd_argument( bound_handler( *this, &t_creature_select_window::button_click ),
		                        stack );
	button->set_click_handler( handler );
	m_buttons.push_back( button );
	if (m_single_select)
		m_button_group.add( button );
	m_creatures.push_back( stack );

	// now arrange all the buttons
	int         i;
	std::string layer_name;

	for (i = 0; i < m_buttons.size(); i++)
	{
		layer_name = format_string( "hero_%i_of_%i", i + 1, m_buttons.size() );
		point = m_bitmaps->find( layer_name )->get_rect().top_left();
		m_buttons[i]->move( point );
	}
}

// ------------------------------------------------------------
// control which allows you to select one or more creatures
// ------------------------------------------------------------
void t_creature_select_window::enable( int id, bool arg )
{
	if (id < 0 || id >= m_buttons.size())
		return;
	m_buttons[id]->enable( arg );
	if (!arg)
		m_buttons[id]->set_pressed( false );
}

bool t_creature_select_window::is_enabled( int id ) const
{
	if (id < 0 || id >= m_buttons.size())
		return false;
	return m_buttons[id]->is_enabled();
}

bool t_creature_select_window::is_selected( int id ) const
{
	if (id < 0 || id >= m_buttons.size())
		return false;
	return m_buttons[id]->is_pressed();
}

// ------------------------------------------------------------
// control which allows you to select one or more creatures
// ------------------------------------------------------------
void t_creature_select_window::set_help_text( int id, std::string const& text )
{
	if (id < 0 || id >= m_buttons.size())
		return;
	m_buttons[id]->set_right_click_text( text );
	m_buttons[id]->set_help_balloon_text( text );
}

// ------------------------------------------------------------
// control which allows you to select one or more creatures
// ------------------------------------------------------------
void t_creature_select_window::button_click( t_button* button, t_creature_stack* stack )
{
	if (m_single_select && !button->is_pressed())
		return;
	m_handler( this, stack );
}

// ------------------------------------------------------------
// control which allows you to select one or more creatures
// ------------------------------------------------------------
void t_creature_select_window::select_first()
{
	int i;

	for (i = 0; i < m_buttons.size(); i++)
		if (m_buttons[i]->is_enabled())
		{
			m_buttons[i]->set_pressed();
			return;
		}
}

// ------------------------------------------------------------
// control which allows you to select one or more creatures
// ------------------------------------------------------------
void t_creature_select_window::select_all()
{
	int i;

	for (i = 0; i < m_buttons.size(); i++)
		if (m_buttons[i]->is_enabled())
			m_buttons[i]->set_pressed();
}

// ------------------------------------------------------------
// Release all creature buttons.
// ------------------------------------------------------------
void t_creature_select_window::select_none()
{
	int i;

	for (i = 0; i < m_buttons.size(); i++)
		if (m_buttons[i]->is_enabled())
			m_buttons[i]->set_pressed( false );
}

/* -------------------------------------------------------------------*\
**
** Creature toggle window - only select 1 creature.
**
\* -------------------------------------------------------------------*/

/*
** Toggle creatures.  Toggle is sticky, so always true.
*/
t_creature_toggle_window::t_creature_toggle_window( t_screen_point point, t_window* parent )
					: t_creature_select_window( point, true, parent )
{
}

/*
** Control which allows you to toggle one creature.
*/
void t_creature_toggle_window::button_click( t_button* button, t_creature_stack* stack )
{
    int i;
    
	for (i = 0; i < m_buttons.size(); i++)
    {
		if ((m_buttons[i]->is_pressed()) && (m_buttons[i] != button))
        {
			m_buttons[i]->set_pressed( false );
        }
    }
    
	if (m_single_select && !button->is_pressed())
		return;

	m_handler( this, stack );
}

